package net.sea.setup.ui.common;

import static java.awt.event.KeyEvent.VK_ENTER;
import static javax.swing.JFileChooser.APPROVE_OPTION;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 * 文件选择
 *
 * @author 华成伍
 *
 */
public final class SelectFile {

	private OnTopFileChooser dialog = new OnTopFileChooser();

	/**
	 *
	 * @param type
	 *            对话窗口类型
	 * @param multi
	 *            是否多选
	 * @param mode
	 *            选择类型（目录、文件、目录与文件）
	 * @param title
	 *            标题栏文字
	 * @param text
	 *            按钮文字描述
	 * @param filters
	 *            过滤文件名称 {@link FileNameExtensionFilter}
	 */
	public SelectFile(int type, boolean multi, int mode, String title,
					  String text, FileFilter... filters) {
		dialog.setDialogType(type);
		dialog.setMultiSelectionEnabled(multi);
		for (FileFilter filter : filters) {
			dialog.addChoosableFileFilter(filter);
		}
		dialog.setApproveButtonText(text);
		dialog.setApproveButtonMnemonic(VK_ENTER);
		dialog.setDialogTitle(title);
		dialog.setFileSelectionMode(mode);
		dialog.setVisible(true);
	}

	public File getSelectedFile() {
		return dialog.showDialog(null, null) == APPROVE_OPTION ? dialog
				.getSelectedFile() : null;

	}

	public File[] getSelectedFiles() {
		return dialog.showDialog(null, null) == APPROVE_OPTION ? dialog
				.getSelectedFiles() : null;
	}

	public FileFilter getFileFilter() {
		return dialog.getFileFilter();
	}

	public JFileChooser getFileChooser() {
		return dialog;
	}
}
